package com.rabbiter.oes.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.ExamManage;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamManageMapper {
//    @Select("select * from exam_manage")
//    List<ExamManage> findAll();

    @Select("select * from exam_manage")
    IPage<ExamManage> findAll(Page page);

    @Select("select * from exam_manage where examCode = #{examCode}")
    ExamManage findById(String examCode);

    @Delete("delete from exam_manage where examCode = #{examCode}")
    int delete(String examCode);

    @Update("UPDATE exam_manage \n" +
            "SET \n" +
            "    paperId = #{paperId},\n" +
            "    startTime = #{startTime},\n" +
            "    totalTime = #{totalTime},\n" +
            "    grade = #{grade},\n" +
            "    totalScore = #{totalScore},\n" +
            "    type = #{type},\n" +
            "    subject = #{subject}\n" +
            "    endTime = #{endTime}\n" +
            "WHERE examCode = #{examCode}")
    int update(ExamManage exammanage);

    @Options(useGeneratedKeys = true,keyProperty = "examCode")
    @Insert("INSERT INTO exam_manage (paperId, startTime, totalTime, grade, totalScore, type, subject, examCode,endTime) " +
            "VALUES (#{paperId}, #{startTime}, #{totalTime}, #{grade}, #{totalScore}, #{type}, #{subject}, #{examCode},#{endTime})")
    int add(ExamManage exammanage);

    /**
     * 查询最后一条记录的paperId,返回给前端达到自增效果
     * @return paperId
     */
    @Select("SELECT COALESCE(MAX(paperId), 0) FROM exam_manage")
    int findOnlyPaperId();
}
